package org.spiderflow.core.expression;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author zsh
 */
public class ExpressionGlobalVariables {

    private static final Map<String, String> VARIABLES = new HashMap<>();

    private static final ReentrantReadWriteLock READ_WRITE_LOCK = new ReentrantReadWriteLock();

    private ExpressionGlobalVariables() {
    }

    public static void reset(Map<String, String> map) {
        Lock lock = READ_WRITE_LOCK.writeLock();
        lock.lock();
        try {
            VARIABLES.clear();
            VARIABLES.putAll(map);
        } finally {
            lock.unlock();
        }
    }

    public static Map<String, String> getVariables() {
        Lock lock = READ_WRITE_LOCK.readLock();
        lock.lock();
        try {
            return VARIABLES;
        } finally {
            lock.unlock();
        }
    }
}
